package com.bwie.bwedu.presenter.impl;

import android.text.TextUtils;

import com.bwie.bwedu.fragment.PasswordUpdFragment;
import com.bwie.bwedu.model.IPasswordUpdModel;
import com.bwie.bwedu.model.impl.PasswordModelImpl;
import com.bwie.bwedu.presenter.IPasswordUpdPresenter;
import com.bwie.bwedu.utils.ASimpleCacheUtils;

/**
 * Created by Administrator on 2016/7/23.
 */
public class PasswordUpdPresenterImpl implements IPasswordUpdPresenter,IPasswordUpdModel.ChangedListener {
    private PasswordUpdFragment fragment;
    private final PasswordModelImpl model;
    private boolean single=true;

    public PasswordUpdPresenterImpl(PasswordUpdFragment fragment) {
        this.fragment = fragment;
        model = new PasswordModelImpl();
    }

    @Override
    public void pwdUpd(String oldPwd, String newPwd, String newPwdAgain) {
        if (TextUtils.isEmpty(oldPwd)){
            fragment.onFail("请输入原密码");
            return;
        }
        if(!oldPwd.equals(ASimpleCacheUtils.read("password"))){
            fragment.onFail("输入原密码不正确");
            return;
        }
        if (TextUtils.isEmpty(newPwd)||TextUtils.isEmpty(newPwdAgain)){
            fragment.onFail("请输入新密码！");
            return;
        }
        if(newPwd.length()<8){
            fragment.onFail("密码最少数入8位！");
            return;
        }
        if (!newPwd.equals(newPwdAgain) ){
            fragment.onFail("两次密码不一致");
            return;
        }
        if (single){
            ASimpleCacheUtils.write("password",newPwd);
            model.pwdUpd(newPwd, this);
            single=false;
        }
    }

    @Override
    public void onChangedSuccess() {
        fragment.onSuccess();
        single=true;
    }

    @Override
    public void onChangedFail(String info) {
        fragment.onFail(info);
        single=true;
    }
}
